package com.talent.jiwen.my;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

public class ChangeInfoActivity extends BaseActivity {
    @BindView(R.id.delIv)
    ImageView delIv;
    @BindView(R.id.contentEt)
    EditText contentEt;

    private static final String PARAM_INFO = "param.info";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_change_info;
    }

    @Override
    public String titleName() {
        return "修改名字";
    }

    @Override
    protected void initData() {
        String info = getIntent().getStringExtra(PARAM_INFO);
        contentEt.setText(info);
        if(info.length() > 8){
            contentEt.setSelection(8);
        }else{
            contentEt.setSelection(info.length());
        }

        mHeadRightText.setVisibility(View.VISIBLE);
        mHeadRightText.setTextColor(ContextCompat.getColor(ChangeInfoActivity.this , R.color.common_app_color));
        mHeadRightText.setText("确定");
        mHeadRightText.setTextSize(TypedValue.COMPLEX_UNIT_SP , 15);
        mHeadRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Validators.isEmpty(contentEt.getText().toString())){
                    updateStudentInfo("" , contentEt.getText().toString());
                }else{
                    showToast("请输入名字");
                }
            }
        });
    }

    @OnClick(R.id.delIv)
    public void onViewClicked() {
        contentEt.setText("");
    }

    /**更新学生个人信息
     *
     * @param userImg
     * @param name
     */
    private void updateStudentInfo(final String userImg , final String name){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.getUserId());
        if (!Validators.isEmpty(userImg)){
            map.put("studentHeadImage" , userImg);
        }

        if (!Validators.isEmpty(name)){
            map.put("studentName" , name);
        }

        Observable observable = ApiFactory.getEducationApiSingleton().updateStudentInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(ChangeInfoActivity.this) {
            @Override
            protected void _onNext(Void v) {
                if (!Validators.isEmpty(userImg)){
                    SPUtil.stringIn(SPConstant.USER_HEAD_URL , userImg);
                }

                if (!Validators.isEmpty(name)){
                    SPUtil.stringIn(SPConstant.USER_NAME , name);
                }

                EventBus.getDefault().post(PersonInfoActivity.REFRESH_INFO);

                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);
    }

    /**启动该页面
     *
     * @param context
     * @param info
     */
    public static void startChangeInfoActivity(Context context , String info){
        Intent intent = new Intent();
        intent.putExtra(PARAM_INFO , info);
        intent.setClass(context , ChangeInfoActivity.class);
        context.startActivity(intent);
    }
}
